package com.system.xingzhi.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.PageMapper;
import com.system.xingzhi.dao.mapper.RoleAndPageMapper;
import com.system.xingzhi.dao.mapper.RoleMapper;
import com.system.xingzhi.dao.mapper.RolePermissionMapper;
import com.system.xingzhi.model.*;
import com.system.xingzhi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    PageMapper pageMapper;

    @Autowired
    RoleAndPageMapper roleAndPageMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String roleId) {
        ReturnData returnData=new ReturnData();
        try {
            roleMapper.deleteByPrimaryKey(roleId);
            returnData.setCode(0);
            returnData.setDetail("删除角色成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("删除角色失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData insert(Role record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(roleMapper.insert(record));

            //根据新角色，生成新角色权限根节点
            RolePermission rolePermission=new RolePermission();
            rolePermission.setRoleId(record.getRoleId());
            rolePermission.setRoleName(record.getRoleName());
            rolePermission.setNodeName(record.getRoleName());
            rolePermission.setNodeType("根节点");
            rolePermissionMapper.insert(rolePermission);

            returnData.setCode(0);
            returnData.setDetail("添加角色成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("添加角色失败");
            return returnData;
        }
        return returnData;

    }

    @Override
    public int insertSelective(Role record) {
        return roleMapper.insertSelective(record);
    }

    @Override
    public Role selectByPrimaryKey(String roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(Role record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(roleMapper.updateByPrimaryKey(record));
            returnData.setCode(0);
            returnData.setDetail("更新角色角色成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("更新角色失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData selectByParameter(Map<String, String> map,Integer pageNum, Integer pageSize) {
        ReturnData returnData=new ReturnData();
        try {
            //分页
            PageHelper.startPage(pageNum, pageSize);

            List<Role> queryResult = roleMapper.selectByParameter(map);
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("查询用户数据成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("查询角色失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData addRolePage(String roleId, String pageId) {
        ReturnData returnData=new ReturnData();
        if (roleId==null||pageId==null){
            returnData.setCode(1);
            returnData.setDetail("字段为空，请确认后在试");
            return returnData;
        }
        Map<String,String> map=new HashMap<>();
        map.put("pageId",pageId);
        map.put("roleId",roleId);
        RoleAndPage data=roleAndPageMapper.selectByPageAndRole(map);
        if (data!=null){
            returnData.setDetail("数据库中已存在");
            returnData.setCode(1);
            return  returnData;
        }
        try {
            Role role=roleMapper.selectByPrimaryKey(roleId);
            Page page=pageMapper.selectByPrimaryKey(pageId);
            if(role!=null||page!=null){
                RoleAndPage roleAndPage=new  RoleAndPage();
                roleAndPage.setPageId(page.getPageId());
                roleAndPage.setPageName(page.getPageName());
                roleAndPage.setRoleId(role.getRoleId());
                roleAndPage.setRoleName(role.getRoleName());
                roleAndPageMapper.insert(roleAndPage);
                returnData.setCode(0);
                returnData.setDetail("添加角色页面成功");
            }
        }catch (Exception E){
            returnData.setCode(1);
            returnData.setDetail("数据库不存在当前角色或页面，请重试");
            return returnData;
        }
        return returnData;
    }

    /**
     *
     * @param rolePageId
     * @return
     */
    @Override
    public ReturnData deleteRolePage(String rolePageId) {
        ReturnData returnData=new ReturnData();
        try {
            int res=roleAndPageMapper.deleteByPrimaryKey(rolePageId);
            if(res==1){
                returnData.setCode(0);
                returnData.setDetail("删除页面成功");
            }
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail(e.toString());
        }
        return returnData;
    }


    /**
     * 通过角色id和角色名.查询角色页面配置信息
     * @param map
     * @return ReturnData
     */
    @Override
    public ReturnData queryRolePage(Map<String, String> map) {
        ReturnData returnData=new ReturnData();
        try {
            List<RoleAndPage> res=roleAndPageMapper.queryRolePage(map);
            returnData.setCode(0);
            returnData.setData(res);
            returnData.setDetail("查询成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail(e.toString());
        }
        return returnData;
    }
}
